package com.yymmarket.forum.bbs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.yymmarket.util.PropertiesUtil;

public class ContentBean{

	private int id;
	private int bbsId;
	private String title;
	private String content;
	private int userId;
	private String userName;
	private int replyCount;
	private int readCount;
	private Date createTime;
	private short deleteMark;
	private int deleteUserId;
	private Date lastReplyTime;
	private String attach;
	private short type;
	private short readonly;
	private short markTop;
	private int mark;
	private int makr1;

	public static int READ_UPDATE = Integer.valueOf(PropertiesUtil.getProperties("forum.properties").get("bbs.content.read_update").toString());
	
	public static int REPLY_UPDATE = Integer.valueOf(PropertiesUtil.getProperties("forum.properties").get("bbs.content.reply_update").toString());
	
	AtomicInteger readCounter = new AtomicInteger(0);
	AtomicInteger replyCounter = new AtomicInteger(0);
	
	private List<Long> replyIdList;// = Collections.synchronizedList(new ArrayList<Integer>());
	     
    public List<Long> getReplyIdList() {
    	if(replyIdList == null) {
    		replyIdList = Collections.synchronizedList(new ArrayList<Long>());
    		
    		List<Long> list = ContentReplyService.getInstance().getReplysByContentId(this.id,true);
    		
    		for (Long long1 : list) {
				replyIdList.add(long1);
			}	
    	}
		return replyIdList;
	}
    
    public static void main(String[] args) {
//		List list = new ArrayList();
//		list.add(new Integer(1));
//		list.add(new Integer(2));
//		list.add(new Integer(3));
//		System.out.println(list.subList(0, 3).size());
	}
    
    public List<ContentReplyBean> getReplyIdList(int start,int limit,boolean asc) {
    	List<Long> replyIdList = getReplyIdList();
    	//System.out.println(replyIdList.size());
    	List<Long> list = null;
    	int size = replyIdList.size();
    	//System.out.println("size:"+size);
    	//System.out.println("start:"+start);
    	//System.out.println("limit:"+limit);
    	if(start >= size) {
    		return new ArrayList<ContentReplyBean>();
    	}
    	
    	if(start+limit > size) {
    		if(asc) {
    			//int index = start+limit < size?size:start+limit;
    			list = replyIdList.subList(start, size);
    		} else {
    			list = replyIdList.subList(0, size-start);
    		}
    	} else {
    		if(asc) {
        		list = replyIdList.subList(start, start+limit);
        	} else {
        		list = replyIdList.subList(size-start-limit, size-start);
        	}
    	}
    		
    	
    	
    	List<ContentReplyBean> replyList = new ArrayList<ContentReplyBean>();
		for (Long id : list) {
			if(asc) {
				replyList.add(ContentReplyService.getInstance().getReplyBeanById(id));
			} else {
				replyList.add(0,ContentReplyService.getInstance().getReplyBeanById(id));
			}
			
		}
		
		return replyList;
    }

	public void setReplyIdList(List<Long> replyIdList) {
		this.replyIdList = replyIdList;
	}

	public int incrementRead() {
         return readCounter.incrementAndGet();     
     }
     
     public void reset(){
    	 boolean flag;
	   	 do {     
	   		int result = readCounter.get();
	   		flag = readCounter.compareAndSet(result,0);
	   		if(flag) {
	   			readCount += result;
	   		}
	   	 } while (!flag);
	   	 
	   	 flag = false;
	   	 do {     
	   		int result = replyCounter.get();
	   		flag = replyCounter.compareAndSet(result,0);
	   		if(flag) {
	   			replyCount += result;
	   		}
	   	 } while (!flag);
     }
     
     public int incrementReply(){
    	 return replyCounter.incrementAndGet();
     }
	
	public boolean isReadOnly(){
		return readonly == 1;
	}
	
	public boolean isPrime(){
		return type == 1;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getBbsId() {
		return bbsId;
	}
	public void setBbsId(int bbsId) {
		this.bbsId = bbsId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public int getReplyCount() {
		return replyCount + replyCounter.get();
	}
	public void setReplyCount(int replyCount) {
		this.replyCount = replyCount;
	}
	public int getReadCount() {
		return readCount + readCounter.get();
	}
	public void setReadCount(int readCount) {
		this.readCount = readCount;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public short getDeleteMark() {
		return deleteMark;
	}
	public void setDeleteMark(short deleteMark) {
		this.deleteMark = deleteMark;
	}
	public int getDeleteUserId() {
		return deleteUserId;
	}
	public void setDeleteUserId(int deleteUserId) {
		this.deleteUserId = deleteUserId;
	}
	public Date getLastReplyTime() {
		return lastReplyTime;
	}
	public void setLastReplyTime(Date lastReplyTime) {
		this.lastReplyTime = lastReplyTime;
	}
	public String getAttach() {
		return attach;
	}
	public void setAttach(String attach) {
		this.attach = attach;
	}
	public short getType() {
		return type;
	}
	public void setType(short type) {
		this.type = type;
	}
	public short getReadonly() {
		return readonly;
	}
	public void setReadonly(short readonly) {
		this.readonly = readonly;
	}
	public short getMarkTop() {
		return markTop;
	}
	public void setMarkTop(short markTop) {
		this.markTop = markTop;
	}
	public int getMark() {
		return mark;
	}
	public void setMark(int mark) {
		this.mark = mark;
	}
	public int getMakr1() {
		return makr1;
	}
	public void setMakr1(int makr1) {
		this.makr1 = makr1;
	}

	public int getReadCounter() {
		return readCounter.get();
	}

	public int getReplyCounter() {
		return replyCounter.get();
	}
}
